﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class Operators : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] roles = Roles.GetRolesForUser(Page.User.Identity.Name);

                if (!roles.Contains("Manager"))
                {
                    Response.Redirect("Default.aspx");
                }

                LoadGridView();
            }
        }

        private void LoadGridView()
        {
            var displayList = AdministratorBLL.GetAllAdministrators().Where(ad => ad.Role == "Operator")
                .Join(StaffBLL.GetAllStaffs(), admin => admin.StaffId, staff => staff.StaffId, (admin, staff) => new
                {
                    staff.StaffId,
                    staff.FirstName,
                    staff.TelephoneNo,
                    admin.UserCredentials.Username
                }).OrderByDescending(s => s.StaffId).ToList();

            Session["Operators"] = displayList;

            gvAdmins.DataSource = displayList;
            gvAdmins.DataBind();
        }

        protected void ibtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            int staffId;
            if (!int.TryParse(tbSearch.Text, out staffId))
            {
                LoadGridView();
            }
            else
            {
                Staff staff = StaffBLL.GetStaffById(staffId);

                if (staff == null)
                {
                    gvAdmins.DataSource = null;
                }
                else
                {
                    Administrator admin = AdministratorBLL.GetAdministrator(staffId);

                    if (admin != null && admin.Role == "Operator")
                    {
                        Administrator administrator = new Administrator(admin, staff);
                        List<Administrator> administrators = new List<Administrator> {administrator};
                        var displayList = administrators.Select(ad => new
                        {
                            ad.StaffId,
                            ad.FirstName,
                            ad.TelephoneNo,
                            ad.UserCredentials.Username
                        }).ToList();

                        Session["Operators"] = displayList;
                        gvAdmins.DataSource = displayList;
                    }
                    else
                    {
                        gvAdmins.DataSource = null;
                    }
                }

                gvAdmins.DataBind();
            }
        }

        protected void gvAdmins_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAdmins.PageIndex = e.NewPageIndex;
            LoadGridView();
        }

        protected void ibDelete_OnClick(object sender, ImageClickEventArgs e)
        {
            int staffId = Convert.ToInt32(Request.QueryString["StaffId"]);
            string username = AdministratorBLL.GetAdministrator(staffId).UserCredentials.Username;
            StaffBLL.RemoveStaff(staffId);
            UserBLL.RemoveUser(username);
            LoadGridView();
        }

        protected void ibBan_OnClick(object sender, ImageClickEventArgs e)
        {
            UserBLL.ChangeUserStatus(Request.QueryString["username"], false);
            gvAdmins.DataSource = Session["Operators"];
            gvAdmins.DataBind();
        }

        protected void ibUnban_OnClick(object sender, ImageClickEventArgs e)
        {
            UserBLL.ChangeUserStatus(Request.QueryString["username"], true);
            gvAdmins.DataSource = Session["Operators"];
            gvAdmins.DataBind();
        }
    }
}